//
//  SQReleaseViewController.m
//  energy
//
//  Created by 苏青 on 2021/9/29.
//

#import "SQReleaseViewController.h"

#import "SQAddReleaseViewController.h"
#import "SQLookPlanViewController.h"
#import "SQAccidentTypesViewController.h"


#import "SQReleaseModel.h"
#import "SQAddReleaseModel.h"

@interface SQReleaseViewController ()<BaseTableVCInterface>
@property ( strong ,nonatomic) NSArray *adjustmentArray;
/**应急预案id*/
@property ( strong ,nonatomic) NSArray *accidentTypesArray;
@end

@implementation SQReleaseViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"应急响应";
    [self getReleaseData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getReleaseData) name:@"addRelease" object:nil];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getReleaseData];
    }];

   
    LC_WEAK_SELF
    self.cellTitleIndex = ^(NSString * str, int key) {
        SQReleaseModelData * dataModel = weakSelf.dataSouece[key];
        if ([str isEqualToString:@"add"]) {
//            weakSelf.accidentTypesArray = [dataModel.er_plan_id componentsSeparatedByString:@","];
            SQAccidentTypesViewController * accidentTypesVC = SQAccidentTypesViewController.new;
            if (weakSelf.accidentTypesArray.count != 0) {
                accidentTypesVC.accidentTypeIDs = weakSelf.accidentTypesArray;
                
            }
            accidentTypesVC.accidentTypesBack = ^(id  _Nonnull str, id  _Nonnull josn, id  _Nonnull accidentTypesModel) {
                [SQAddReleaseModel getDataWithParams:@{@"er_id":dataModel.er_id,@"er_grade_id":dataModel.er_grade_id,@"er_grade_text":dataModel.er_grade_text,@"er_plan_id":josn,@"er_plan_text":str,@"er_responsible_id":dataModel.er_responsible_id,@"er_responsible_text":dataModel.er_responsible_text,@"er_status":dataModel.er_status,@"er_publish_person":dataModel.er_publish_person,@"er_publish_organize":dataModel.er_publish_organize,@"er_enterprise":MEnterpriseId,@"er_alarm_person":@""} WithCallBack:^(SQAddReleaseModel * data) {
                    [CCToast showMessage:@"应急响应类型更新完成"];
                    [weakSelf getReleaseData];
                    
                }];
            };
            [weakSelf pushVC:accidentTypesVC];
        }else if ([str isEqualToString:@"adjustment"]){
            
            if ([dataModel.er_status isEqualToString:@"1"]) {
                NSString *strUrl = [dataModel.er_responsible_text stringByReplacingOccurrencesOfString:@"、" withString:@"-"];
                NSArray *telAndPeoplelArr = [strUrl componentsSeparatedByString:@","];
                
                NSArray *a1 = [dataModel.er_responsible_text componentsSeparatedByString:@","];
                  NSMutableArray *all = @[].mutableCopy;
                  for (NSString *str in a1) {
                      NSArray *a2 = [str componentsSeparatedByString:@"、"];
                      [all addObject:a2.lastObject];
                  }
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"报警电话"
                                                                                   message:nil
                                                                            preferredStyle:UIAlertControllerStyleActionSheet];
                for (int i = 0; i < telAndPeoplelArr.count; i ++) {
//                    SQKeyAlarmModelData * dataModel = weakSelf.keyAlarmModel.data[i];
                    UIAlertAction* telAction = [UIAlertAction actionWithTitle:telAndPeoplelArr[i] style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {

                        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",all[i]]];

                        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {

                        }];
                        

                    }];
                    [alert addAction:telAction];
                }
                
                
                UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          NSLog(@"action = %@", action);
                                                                      }];
                [alert addAction:cancelAction];
    //
                [weakSelf presentViewController:alert animated:YES completion:nil];
                
            }else if ([dataModel.er_status isEqualToString:@"0"]){
                if ([dataModel.er_grade_id isEqualToString:@"1"]) {
                    weakSelf.adjustmentArray = @[@"II级",@"III级",@"IV级"];
                }else if ([dataModel.er_grade_id isEqualToString:@"2"]){
                    weakSelf.adjustmentArray = @[@"I级",@"III级",@"IV级"];
                }else if ([dataModel.er_grade_id isEqualToString:@"3"]){
                    weakSelf.adjustmentArray = @[@"I级",@"II级",@"IV级"];
                }else if ([dataModel.er_grade_id isEqualToString:@"4"]){
                    weakSelf.adjustmentArray = @[@"I级",@"II级",@"III级"];
                }
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"应急响应等级"
                                                                                   message:nil
                                                                            preferredStyle:UIAlertControllerStyleActionSheet];
                for (int i = 0; i < weakSelf.adjustmentArray.count; i ++) {
                  
                    UIAlertAction* adjustmentAction = [UIAlertAction actionWithTitle:weakSelf.adjustmentArray[i] style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {
                        NSString * er_grade_id;
                        if ([weakSelf.adjustmentArray[i] isEqualToString:@"I级"]) {
                            er_grade_id = @"1";
                        }else if ([weakSelf.adjustmentArray[i] isEqualToString:@"II级"]){
                            er_grade_id = @"2";
                        }else if ([weakSelf.adjustmentArray[i] isEqualToString:@"III级"]){
                            er_grade_id = @"3";
                        }else if ([weakSelf.adjustmentArray[i] isEqualToString:@"IV级"]){
                            er_grade_id = @"4";
                        }
                        [SQAddReleaseModel getDataWithParams:@{@"er_id":dataModel.er_id,@"er_grade_id":er_grade_id,@"er_grade_text":weakSelf.adjustmentArray[i],@"er_plan_id":dataModel.er_plan_id,@"er_plan_text":dataModel.er_plan_text,@"er_responsible_id":dataModel.er_responsible_id,@"er_responsible_text":dataModel.er_responsible_text,@"er_status":dataModel.er_status,@"er_publish_person":dataModel.er_publish_person,@"er_publish_organize":dataModel.er_publish_organize,@"er_enterprise":MEnterpriseId,@"er_alarm_person":@""} WithCallBack:^(SQAddReleaseModel * data) {
                            [CCToast showMessage:@"应急响应等级更新完成"];
                            [weakSelf getReleaseData];
                            
                        }];
                      
                    }];
                    [alert addAction:adjustmentAction];
                }
                
                
                UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                    NSLog(@"action = %@", action);
                }];
                [alert addAction:cancelAction];
    //
                [weakSelf presentViewController:alert animated:YES completion:nil];
                
            }
            
        }else if ([str isEqualToString:@"end"]){
            if ([dataModel.er_status isEqualToString:@"1"]) {
                SQLookPlanViewController * lookPlanVC = SQLookPlanViewController.new;
                lookPlanVC.erp_er = dataModel.er_id;
                lookPlanVC.er_grade_id = dataModel.er_grade_id;
                [weakSelf pushVC:lookPlanVC];
            }else if ([dataModel.er_status isEqualToString:@"0"]){
                [SQAddReleaseModel getDataWithParams:@{@"er_id":dataModel.er_id,@"er_grade_id":dataModel.er_grade_id,@"er_grade_text":dataModel.er_grade_text,@"er_plan_id":dataModel.er_plan_id,@"er_plan_text":dataModel.er_plan_text,@"er_responsible_id":dataModel.er_responsible_id,@"er_responsible_text":dataModel.er_responsible_text,@"er_status":@"1",@"er_publish_person":dataModel.er_publish_person,@"er_publish_organize":dataModel.er_publish_organize,@"er_enterprise":MEnterpriseId,@"er_alarm_person":@""} WithCallBack:^(SQAddReleaseModel * data) {
                    [CCToast showMessage:@"应急响应结束成功"];
                    [weakSelf getReleaseData];
                    
                }];
            }
           
            
        }
    };
   
    
}

-(void)getReleaseData{
    [SQReleaseModel getDataWithParams:@{@"er_enterprise":MEnterpriseId,@"type":@"1"} WithCallBack:^(SQReleaseModel * data) {
        [self detailModelwithArr:data.data withCallBack:nil];
        [self reloadTableView];
        
    }];
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.dataSouece[indexPath.row];
}



-(NSInteger)rowsCount:(NSInteger)section{
    return self.dataSouece.count;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQReleaseTableViewCell"]];
}


- (IBAction)clickAddRelease:(UIButton *)sender {
    SQAddReleaseViewController * addReleaseVC = SQAddReleaseViewController.new;
    addReleaseVC.apr_alarm_person = @"";
    [self pushVC:addReleaseVC];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
